package conson.tech.manage.activity.service.impl;

import cn.hutool.json.JSONUtil;
import conson.tech.core.response.Response;
import conson.tech.manage.activity.entity.dto.MessagePushDTO;
import conson.tech.manage.activity.entity.pojo.TicketInfoRecord;
import conson.tech.manage.activity.service.MessagePushFeignService;
import conson.tech.manage.activity.service.MessagePushService;
import conson.tech.manage.activity.service.TicketRecordService;
import conson.tech.manage.api.activity.constant.ActivityConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: MessagePushServiceImpl
 * Description:
 * Author: HXL
 * Date: 2020/5/28 2:17 下午
 * Version: 1.0
 */
@Slf4j
@RefreshScope
@Service
public class MessagePushServiceImpl implements MessagePushService {
    @Resource
    private MessagePushFeignService messagePushFeignService;
    @Autowired
    private TicketRecordService ticketRecordService;
    @Value("${msg.clientId}")
    private String clientId;
    @Value("${msg.push.templateCode.set}")
    private String templateCodeSet;
    @Value("${msg.push.templateCode.tcc}")
    private String templateCodeTcc;
    @Value("${msg.push.templateCode.hsyc}")
    private String templateCodeHsyc;
    @Value("${msg.push.templateCode.gj}")
    private String templateCodeGj;
    @Value("${msg.push.templateCode.bwg}")
    private String templateCodeBwg;

    @Override
    public void pushMessage(String busTicketId) {
        try {
            log.info("=========进入发送推送消息方法==============");
            TicketInfoRecord record = ticketRecordService.queryTicketInfoRecordByBusTicketId(busTicketId);
            if (record != null ) {
                MessagePushDTO dto = new MessagePushDTO();
                dto.setClientId(clientId);
                //票券类型：隧e通通行券: 1 公交充值优惠券:2 停车场优惠券:3 海水浴场免费更衣券:4 隧道博物馆：5
                if (record.getTicketType().intValue() == ActivityConstant.TICKET_TYPE_ONE){
                    dto.setTemplateCode(templateCodeSet);
                }else if (record.getTicketType().intValue() == ActivityConstant.TICKET_TYPE_TWO){
                    dto.setTemplateCode(templateCodeGj);
                }else if (record.getTicketType().intValue() == ActivityConstant.TICKET_TYPE_THREE){
                    dto.setTemplateCode(templateCodeTcc);
                }else if (record.getTicketType().intValue() == ActivityConstant.TICKET_TYPE_FOUR){
                    dto.setTemplateCode(templateCodeHsyc);
                }else if (record.getTicketType().intValue() == ActivityConstant.TICKET_TYPE_FIVE){
                    dto.setTemplateCode(templateCodeBwg);
                }
                dto.setTemplateParam(null);
                List<Long> userIdList = new ArrayList<>();
                userIdList.add(record.getUserId());
                dto.setSendUserId(userIdList);
                log.info("票券推送数据为:[{}]", JSONUtil.toJsonStr(dto));
                Response response = messagePushFeignService.pushAsyncSend(dto);
                log.info("推送中心返回的数据为:[{}]", JSONUtil.toJsonStr(response));
                log.info("=========退出发送推送消息方法==============");
            }
        } catch (Exception e) {
           log.error("票券推送消息出现异常",e);
        }

    }
}
